﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using DG.Tweening;
using UnityEngine;
public class GameProxy {
    public ControlBox ControlBox;
    public UiMenu UiMenu;
    public UiControl UiControl;
    public List<bgCubeOne> arrBgCubeOne = new List<bgCubeOne> (); //记录地板
    public List<FileInfo> arrFileInfo = new List<FileInfo> ();
    public int oppenFileIndex = 0;
    public toolOne copyToolOne;
    public bool hasToolDraging = false;//记录是否有方块正被拖动
    public GameProxy () {
        setData ();
    }
    public void setData () {
        arrFileInfo = LoadList ();
        if (UiMenu != null) {
            UiMenu.ScrollTool.refresh ();
        }
    }
    public void saveData (saveData data) {
        string fileName = arrFileInfo[oppenFileIndex].Name;
        Global.IO.WriteToPersistentData (fileName, data);
    }
    public void saveData () {
        string fileName = arrFileInfo[oppenFileIndex].Name;
        saveData data = ControlBox.getSaveData ();
        Global.IO.WriteToPersistentData (fileName, data);
    }
    public void saveData (string fileName, saveData data) {
        Global.IO.WriteToPersistentData (fileName, data);
    }
    public string GetLevelJsonFileName (int level) {
        return $"Level_{level}.json";
    }
    public int GetLevelByFileName (string fileName) {
        return int.Parse (System.Text.RegularExpressions.Regex.Replace (fileName, @"[^0-9]+", ""));
    }
    public bool IsLevelJsonFileName (string fileName) {
        return fileName.StartsWith ("Level_") && fileName.EndsWith (".json");
    }
    public bool IsLevelJsonFileExist (int level) {
        return Global.IO.IsFileExistInPersistentDataPath (GetLevelJsonFileName (level));
    }
    public void RemoveFile (string fileName) {
        if (Global.IO.IsFileExistInPersistentDataPath (fileName)) {
            Global.IO.RemoveFile (fileName);
        }
    }
    public saveData ReadLevelData (int level) {
        return Global.IO.ReadFromPersistentData<saveData> (GetLevelJsonFileName (level));
    }
    //重命名
    public void reNameFile (int beforeLevel, int toLevel) {
        if (beforeLevel == toLevel) {
            UiControl.showError ("同名改你妹啊");
            return;
        }
        if (IsLevelJsonFileExist (toLevel)) {
            UiControl.showError ("该ID已存在");
            return;
        }
        if (toLevel < 0) {
            UiControl.showError ("非法ID");
            return;
        }
        saveData data = ReadLevelData (beforeLevel);
        RemoveFile (GetLevelJsonFileName (beforeLevel));
        saveData (GetLevelJsonFileName (toLevel), data);

    }
    //获取文件夹下的文件
    // MAX -> 0 的顺序
    List<FileInfo> LoadList () {
        var fullPath = Application.persistentDataPath;
        List<FileInfo> cachedFiles = new List<FileInfo> ();
        if (Directory.Exists (fullPath)) {
            DirectoryInfo direction = new DirectoryInfo (fullPath);
            FileInfo[] files = direction.GetFiles ("*", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < files.Length; i++) {
                var file = files[i];
                if (IsLevelJsonFileName (file.Name)) {
                    cachedFiles.Add (file);
                }
            }
            cachedFiles.Sort ((FileInfo f1, FileInfo f2) => {
                var fn1 = GetLevelByFileName (f1.Name);
                var fn2 = GetLevelByFileName (f2.Name);
                return fn1 - fn2;
            });
        }
        return cachedFiles;
    }
    //创建新的关卡
    public void newLevel () {
        int index = 0;
        while (IsLevelJsonFileExist (index)) {
            index += 1;
        }
        creatorLevel (index);
        setData ();
    }
    //创建新的关卡
    public void newCopy (int copyLevel) {
        int index = 0;
        while (IsLevelJsonFileExist (index)) {
            index += 1;
        }
        saveData data = ReadLevelData (copyLevel);
        creatorLevel (index, data);
        setData ();
    }
    //创建文件
    public void creatorLevel (int level) {
        saveData data = new saveData ();
        string fillName = GetLevelJsonFileName (level);
        Global.IO.WriteToPersistentData (fillName, data);
    }
    //创建文件
    public void creatorLevel (int level, saveData data) {
        string fillName = GetLevelJsonFileName (level);
        Global.IO.WriteToPersistentData (fillName, data);
    }
    //根据索引获取json名
    public string getNameByIndex (int index) {
        return arrFileInfo[index].Name;
    }
    public int getLevelByIndex (int index) {
        return GetLevelByFileName (arrFileInfo[index].Name);
    }
}